<template>
  <div class="container">
    <base-title base-title="发布说说" />
    <textarea class="text" rows="10" v-model="messageContent" placeholder="说点什么吧~" :maxlength="maxlength"></textarea>
    <div class="main">
      <Emoji @handle-emotion="handleEmotion" />
      <div class="font">
        <span>{{ messageContent.trim().length }}</span>
        <span>/</span>
        <span>{{ maxlength }}</span>
      </div>
      <div class="create">
        <el-button type="primary" @click="createMessage">发布</el-button>
      </div>
    </div>
    <!-- <p>效果显示框</p> -->
    <!-- <div class="text-place"> -->
    <!-- /\#[\u4E00-\u9FA5]{1,3}\;/gi 匹配出含 #XXX; 的字段 -->
    <!-- <p v-html="messageContent.replace(/\#[\u4E00-\u9FA5]{1,3}\;/gi, emotion)"></p> -->
    <!-- </div> -->
  </div>
</template>

<script lang="ts" name="messageEditor" setup>
  import Emoji from '@/components/emotion/Emoji.vue'
  import notification from '@/utils/notification'
  import { createMessageRequest } from '@/network/api/message-api'
  import { ref } from 'vue'

  const messageContent = ref('')
  const handleEmotion = (i: string) => {
    messageContent.value += i
  }

  const maxlength = 200
  const createMessage = async () => {
    if (!messageContent.value.trim()) return notification.warning('内容不能为空~')
    if (messageContent.value.trim().length > maxlength) return notification.warning('不能超过最大长度~')
    const result = await createMessageRequest(messageContent.value)
    if (result.status === 200) {
      notification.success(result.msg)
      messageContent.value = ''
    }
  }
</script>

<style scoped lang="less">
  .main {
    position: relative;
    .font {
      position: absolute;
      top: -40px;
      right: 12px;
      font-size: 14px;
      color: #999;
    }
    .create {
      position: absolute;
      top: -2px;
      right: 7px;
      .el-switch {
        margin: 0 10px;
      }
    }
  }
  .text {
    display: block;
    margin: 0 auto;
    margin-bottom: 10px;
    width: 100%;
    resize: none;
    box-sizing: border-box;
    padding: 5px 10px;
    border-radius: 8px;
    border: none;
    outline: none;
    font-size: 16px;
    background-color: #f0f1f4;
  }
  .text-place {
    height: 80px;
    box-sizing: border-box;
    padding: 5px 10px;
    border-radius: 8px;
    background: #ddd5d5;
  }
  .text-place p {
    display: flex;
    align-items: center;
    margin: 0;
  }
</style>
